package cn.gok.controller;

import cn.gok.entity.Student;
import cn.gok.service.IStudentService;
import cn.gok.service.impl.StudentServiceImpl;
import cn.gok.util.ParseRequestParameterUtil;
import cn.gok.util.ResponseUtil;
import cn.gok.util.ResultData;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @Description:
 * @Author: chen fan
 * @Date: 2022年10月18日 15:40:35
 */

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private IStudentService studentService = new StudentServiceImpl();


    @Override
    protected void service(HttpServletRequest req,
                           HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");

        switch (action){
            case "login":login(req, resp);break;
            case "updatePassword": updatePassword(req, resp);break;
            case "updateStudentInfo": updateStudentInfo(req, resp);break;
            case "getStudentInfo": getStudentInfo(req, resp);break;
            case "querySecurityQuestion":querySecurityQuestion(req,resp);break;
            case "checkSecurityQuestion":checkSecurityQuestion(req,resp);break;
            case "forgetUpdatePassword":forgetUpdatePassword(req,resp);break;

        }
    }

    /**
     * 忘记密码时的修改密码操作
     * @param req
     * @param resp
     */
    private void forgetUpdatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultData resultData = studentService.forgetUpdatePassword(req.getSession().getId(),
                req.getParameter("password"));
        ResponseUtil.send(resp,resultData);
    }


    /**
     * 检查密保问题是否正确
     */
    private void checkSecurityQuestion(HttpServletRequest req,
                                       HttpServletResponse resp)throws IOException {
        String sessionId = req.getSession().getId();
        Integer stuId = Integer.valueOf(req.getParameter("stuId"));
        String question = req.getParameter("question");
        String answer = req.getParameter("answer");
        ResultData data = studentService.checkAnswer(sessionId, stuId, question, answer);
        ResponseUtil.send( resp,data);
    }

    /**
     * 查询用户的密保问题并放回给前端
     */
    private void querySecurityQuestion(HttpServletRequest req,
                                       HttpServletResponse resp) throws IOException{
        ResultData data = studentService.queryQuestion(req.getParameter("stuNameOrPhoneNo"));
        ResponseUtil.send( resp,data);
    }

    private void getStudentInfo(HttpServletRequest req,
                                HttpServletResponse resp) throws IOException{
        Object studentInfo = req.getSession().getAttribute("studentInfo");
        ResultData data = ResultData.createData(HttpStatus.HTTP_OK, studentInfo);
        ResponseUtil.send( resp,data);
    }


    private void updateStudentInfo(HttpServletRequest req, HttpServletResponse resp) {
        String studentName = req.getParameter("studentName");
    }

    private void updatePassword(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void login(HttpServletRequest req,
                       HttpServletResponse resp) throws IOException {

        String studentName = req.getParameter("stuName");
        String password = req.getParameter("password");


        ResultData resultData = studentService.login(studentName, password);
        //如果用户登录成功，则需要将用户信息存入到session中
        if ((Integer) resultData.get("code") == ResultData.LOGIN_SUCCESS){
            HttpSession session = req.getSession();
            session.setAttribute("studentInfo", resultData.get("data"));
        }
        ResponseUtil.send( resp,resultData);
    }
}
